FrameLib  0.7
DSP processing with frames of arbitrary timing and length
FrameLib_Context.h
Go to the documentation of this file.
1 
2 #ifndef FRAMELIB_CONTEXT_H
3 #define FRAMELIB_CONTEXT_H
4 
5 #include "FrameLib_Global.h"
6 
7 // The Context Object (used to define non-connectable areas in the host environment)
8 // This acts as a proxy to the global object with a suitable reference to the context
9 
11 {
12  // Type definition for concision
13 
14  typedef FrameLib_Global Global;
15 
16  // Comparisions
17 
18  friend bool operator == (const FrameLib_Context& a, const FrameLib_Context& b)
19  {
20  return a.mGlobal == b.mGlobal && a.mReference == b.mReference;
21  }
22 
23  friend bool operator != (const FrameLib_Context& a, const FrameLib_Context& b)
24  {
25  return !(a == b);
26  }
27 
28  // Non-copyable template class for retaining reference counted pointers using RAII
29 
30  template <class T, T *(Global::*getMethod)(void *), void(Global::*releaseMethod)(void *)>
31  class ManagedPointer
32  {
33 
34  public:
35 
36  ManagedPointer(const FrameLib_Context &context) : mGlobal(context.mGlobal), mReference(context.mReference)
37  {
38  mPointer = (mGlobal->*getMethod)(mReference);
39  }
40 
41  ~ManagedPointer()
42  {
43  release();
44  }
45 
46  // Release
47 
48  void release()
49  {
50  if (mGlobal)
51  (mGlobal->*releaseMethod)(mReference);
52  mPointer = NULL;
53  mGlobal = NULL;
54  mReference = NULL;
55  }
56 
57  // Pointer / Bool Conversion
58 
59  T *operator->() { return mPointer; }
60  operator bool() const { return mPointer != NULL; }
61 
62  private:
63 
64  // Deleted
65 
66  ManagedPointer(const ManagedPointer&);
67  ManagedPointer& operator=(const ManagedPointer&);
68 
69  // Member Variables
70 
71  T *mPointer;
72  FrameLib_Global *mGlobal;
73  void *mReference;
74  };
75 
76 public:
77 
78  // Constructor - the reference should be a suitable reference address in the host environment
79 
80  FrameLib_Context(FrameLib_Global *global, void *reference) : mGlobal(global), mReference(reference) {}
81 
82  // Construct one of these objects to retain a relevant object
83 
84  typedef ManagedPointer<FrameLib_LocalAllocator, &Global::getAllocator, &Global::releaseAllocator> Allocator;
85  typedef ManagedPointer<FrameLib_ProcessingQueue, &Global::getProcessingQueue, &Global::releaseProcessingQueue> ProcessingQueue;
86 
87 private:
88 
89  FrameLib_Global *mGlobal;
90  void *mReference;
91 };
92 
93 
94 #endif
FrameLib_Context(FrameLib_Global *global, void *reference)
Definition: FrameLib_Context.h:80
Definition: FrameLib_Global.h:12
Definition: FrameLib_Context.h:10
friend bool operator==(const FrameLib_Context &a, const FrameLib_Context &b)
Definition: FrameLib_Context.h:18
ManagedPointer< FrameLib_ProcessingQueue, &Global::getProcessingQueue, &Global::releaseProcessingQueue > ProcessingQueue
Definition: FrameLib_Context.h:85
friend bool operator!=(const FrameLib_Context &a, const FrameLib_Context &b)
Definition: FrameLib_Context.h:23
ManagedPointer< FrameLib_LocalAllocator, &Global::getAllocator, &Global::releaseAllocator > Allocator
Definition: FrameLib_Context.h:84